package equalsmethod.equalsExercise;

public class Exercise01 {
    public static void main(String[] args) {
        Person person1 = new Person("lihua", 22, '男');
        Person person2 = new Person("lihua", 22, '男');
        System.out.println(person1.equals(person2));
    }
}

class Person{
    private String nama;
    private int age;
    private char gender;

    public Person(String nama, int age, char gender) {
        this.nama = nama;
        this.age = age;
        this.gender = gender;
    }

    //重写object的equals方法
    public boolean equals(Object obj){
        //首先判断是否为同一个对象
        //this指的是调用equals方法的对象(person1)，obj指的是传进来的对象(person2)0
        if(this == obj){
            return true;
        }
        //如果不是同一个对象，则判断是否为obj的运行类型是否为Person类型或者person类型的子类型
        if(obj instanceof Person){
            Person p = (Person) obj;
            return this.nama.equals(p.nama) && this.age == p.age && this.gender == p.gender;

        }
        return false;
    }


    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }
}
